import { Attention, PrivateMsg } from '@entity/chat';
import { Component, Prop, Vue } from 'vue-property-decorator'
import ChatItemView from './history/Index.vue';
import { SimpleEntity } from '@entity/common';
import axios from '@axios/websocket'

@Component({
    components: {
        ChatItemView
    }
})
export class ChatBox extends Vue {

    /** 联系人 */
    @Prop({
        default: () => new Attention(undefined)
    })
    private attention: Attention;

    mounted() {
        let that = this;
        setTimeout(() => {
            let senderId = that.attention.senderId;
            let myId = that.$store.getters.me().id;
            let key = `${Math.min(senderId, myId)}-${Math.max(senderId, myId)}`;
            console.log(key);
            console.log(this.attention)
        }, 1000);
    }

    /**
     * 发送消息
     */
    private sendMsg(): void {
        if (this.attention.draft.isEmpty()) {
            '你尚未输入内容'.notifyWarn();
            return;
        }
        let receiver: SimpleEntity = new SimpleEntity(this.attention.senderId, this.attention.name);
        let privateMsg: PrivateMsg = new PrivateMsg(this.$store.getters.me().id, this.attention.senderId, this.attention.draft, new Date().currentTime());
        this.attention.draft = '';
        axios.private.sendToUser(receiver, privateMsg);
        this.$store.commit('addPrivateMsg', privateMsg);
    }

    /**
     * 输入框处于聚焦状态时的键盘回调函数
     * 按回车时会发送消息
     */
    inputKeyup(event): void {
        if (event.code === 'Enter') {
            this.sendMsg();
        }
    }

}